<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * 系统参数管理
 * @author 		jaypha
 * @qq			168007694
 * @email		psufool@163.com
 * @home page	www.psufool.cn
 */
class Config extends Admin_Controller{
	
	function __construct(){
		parent::__construct();
		$this->load->model('JayphaDB');
		if(is_null($this->adminCurrentTemplate) or empty($this->adminCurrentTemplate)){
			$this->adminCurrentTemplate = 'default';
		}
		$this->dbTablePrefix = $this->config->item('db_table_prefix');
	}
	
	function index(){
		parse_str($this->input->server('QUERY_STRING'),$this->input->get());
		if(!$this->noLoginErr()) return false;
		$data['adminCurrentTemplate'] = $this->adminCurrentTemplate;
		$tvName = 'config';
		$condition = array();
		if($this->input->server('REQUEST_METHOD')=='POST'){
			$active = $this->input->post('active');
			$del = $this->input->post('del');
			$id = $this->input->post('id');
			if(is_array($id)){
				$id = implode(',',$id);
			}
			$data['rDisabled'] = $this->input->post('disabled');
			$data['rName'] = $this->input->post('name');
			$data['rTitle'] = $this->input->post('title');
			$data['rCreateTimeStart'] = $this->input->post('createTimeStart');
			$data['rCreateTimeEnd'] = $this->input->post('createTimeEnd');
		}else{
			$data['rDisabled'] = $this->input->get('disabled');
			$data['rName'] = $this->input->get('name');
			$data['rTitle'] = $this->input->get('title');
			$data['rCreateTimeStart'] = $this->input->get('createTimeStart');
			$data['rCreateTimeEnd'] = $this->input->get('createTimeEnd');
			//list($data['rDisabled'],$data['rName'],$data['rTitle'],$data['rCreateTimeStart'],$data['rCreateTimeEnd']) = explode('~',$this->uri->segment(4,'~~~~'));
		}

		$data['rTitle'] = urldecode($data['rTitle']);

		$condition = array();
		$db_like_key = array();
		$db_like_value = array();
		if(strlen($data['rDisabled']>0)){
			$condition['disabled'] = $data['rDisabled'];
		}
		if(strlen($data['rName'])>0){
			array_push($db_like_key,'upper(name)');
			array_push($db_like_value,strtoupper($data['rName']));
		}
		if(strlen($data['rTitle'])>0){
			array_push($db_like_key,'title');
			array_push($db_like_value,$data['rTitle']);
		}
		if(strlen($data['rCreateTimeStart'])>0){
			$condition['createTime >= '] = $data['rCreateTimeStart'];
		}
		if(strlen($data['rCreateTimeEnd'])>0){
			$condition['createTime <= '] = date('Y-m-d',strtotime($data['rCreateTimeEnd']) + 24*60*60);
		}
		if(count($db_like_key)>0){
			$condition['db_like'] = array_combine($db_like_key,$db_like_value);
		}

		unset($db_like_key);
		unset($db_like_value);
		$where = $condition;

		if($this->input->server('REQUEST_METHOD')=='POST'){
			if(strtoupper($active)=='DEL'){
				if(strlen($del)==0){
					$condition['db_where_in'] = array('id',$id);
				}				
				$operationData = array(
					'adminId'=>$this->session->userdata('adminUserId'),
					'title'=>'删除系统配置选项',
					'url'=>$this->input->server('REQUEST_URI'),
					'remark'=>'删除系统配置选项，删除内容：'.json_encode($this->JayphaDB->getSingleDB($this->dbTablePrefix.$tvName,$condition))
				);
				$this->JayphaDB->setAdminOperation($operationData);
				$this->JayphaDB->delDB($this->dbTablePrefix.$tvName,$condition);
			}
		}

		unset($condition);

		$config['base_url'] = site_url("admin/config/index/");
		$config['total_rows'] = $this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,$where);
		$config['per_page'] = $this->JayphaDB->getConfigValue('adminPageSize');
		$config['uri_segment'] = 4;
		$config['num_links'] = 5;
		//$config['full_tag_open'] = '<div>';
		//$config['full_tag_close'] = '</div>';
		$config['first_link'] = '|<';
		$config['last_link'] = '>|';
		$config['next_link'] = '>>';
		$config['prev_link'] = '<<';

		$this->pagination->initialize($config);

		$data['currentSite'] = '常规管理<span class="split">>></span>系统参数管理<span class="split">>></span>系统配置列表';
		$data['phpSelf'] = $config['base_url'];
		$data['pageTag'] = '系统参数管理';
		$data['editPage'] = site_url('admin/config/edit');
		$data['editTitle'] = '修改系统配置项';
		$data['addTitle'] = '添加系统配置项';
		$data['perm'] = 'systemConfig';
		$data['adminPermission'] = $this->JayphaDB->getAdminPermission($this->session->userdata('adminUserId'));
		$data['tvName'] = $tvName;
		$order = 'oIndex,id';
		$num = $this->uri->segment(4,0);
		$data['configList'] = $this->JayphaDB->getDB($config['per_page'],$num,'',$this->dbTablePrefix.$tvName,$where,$order,false);
		$this->load->view("admin/$this->adminCurrentTemplate/config",$data);
	}
	
	function edit(){
		if(!$this->noLoginErr()) return false;
		$data['adminCurrentTemplate'] = $this->adminCurrentTemplate;
		$tvName = 'config';
		$data['tvName'] = $tvName;
		$data['perm'] = 'systemConfig';
		if($this->input->server('REQUEST_METHOD')=='POST'){
			$id = $this->input->post('id');
			$disabled = $this->input->post('disabled');
			$oIndex = $this->input->post('oIndex');
			$title = $this->input->post('title');
			$name = $this->input->post('name');
			$value = $this->input->post('value');
			$remark = $this->input->post('remark');
			if(strlen($id)>0){
				if($this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,array('id != '=>$id,'upper(name)'=>strtoupper($name)))>0){
					unset($data['errorScript']['lhgdialog_alert_mess']);
					$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
							'width'=>300,
							'height'=>125,
							'title'=>'修改失败',
							'content'=>'<span style="line-height:20px; display:block; padding:0 3px; padding-top:5px;">对不起，变量“<span class="red b">'.$name.'</span>”已经存在，请选择其它变量名称！</span>',
							'url'=>'history.back()',
							'target'=>'_self',
							'conver'=>true,
							'resize'=>false,
							'drag'=>false,
							'btns'=>true,
							'btnText'=>'返回',
							'rightClose'=>false
					));
					$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
					return false;
				}
				$where = array('id'=>$id);
				$updateData = array(
						'disabled'=>$disabled,
						'oIndex'=>$oIndex,
						'title'=>$title,
						'name'=>$name,
						'value'=>$value,
						'remark'=>$remark
				);
				$this->JayphaDB->updateDB($updateData,$this->dbTablePrefix.$tvName,$where);
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>220,
						'height'=>110,
						'title'=>'修改成功 ',
						'content'=>'您的系统项目已成功修改！',
						'url'=>'location.reload',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'关闭',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
			}else{
				$addData = array(
						'disabled'=>$disabled,
						'oIndex'=>$oIndex,
						'title'=>$title,
						'name'=>$name,
						'value'=>$value,
						'remark'=>$remark
				);
				if($this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,array('upper(name)'=>strtoupper($name)))>0){
					unset($data['errorScript']['lhgdialog_alert_mess']);
					$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
							'width'=>300,
							'height'=>125,
							'title'=>'添加失败',
							'content'=>'<span style="line-height:20px; display:block; padding:0 3px; padding-top:5px;">对不起，变量“<span class="red b">'.$name.'</span>”已经存在，请选择其它变量名称！</span>',
							'url'=>'history.back();',
							'target'=>'_self',
							'conver'=>true,
							'resize'=>false,
							'drag'=>false,
							'btns'=>true,
							'btnText'=>'返回',
							'rightClose'=>false
					));
					$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
					return false;
				}
				$this->JayphaDB->insertDB($this->dbTablePrefix.$tvName,$addData,true);
				unset($data['errorScript']['lhgdialog_alert_mess']);
				$data['errorScript'] = array('lhgdialog_alert_mess'=>array(
						'width'=>220,
						'height'=>108,
						'title'=>'添加成功 ',
						'content'=>'您的系统项目已成功添加！',
						'url'=>'location.reload',
						'target'=>'_self',
						'conver'=>true,
						'resize'=>false,
						'drag'=>false,
						'btns'=>true,
						'btnText'=>'关闭',
						'rightClose'=>false
				));
				$this->load->view("admin/$this->adminCurrentTemplate/error",$data);
				return false;
			}
		}else{
			$data['id'] = $this->uri->segment(4);
			if(strlen($data['id'])>0){
				foreach($this->JayphaDB->getDB('','','',$this->dbTablePrefix.$tvName,array('id'=>$data['id']),'oIndex,id',false) as $config){
					$data['oIndex'] = $config->oIndex;
					$data['disabled'] = $config->disabled;
					$data['title'] = $config->title;
					$data['name'] = $config->name;
					$data['value'] = $config->value;
					$data['remark'] = $config->remark;
				}
			}else{
				$data['disabled'] = false;
				$data['title'] = '';
				$data['name'] = '';
				$data['value'] = '';
				$data['remark'] = '';
				$data['oIndex'] = $this->JayphaDB->getDBCount($this->dbTablePrefix.$tvName,'') + 1;
			}
			$this->miniCKEditor = new CKEditor();
			$this->miniCKEditor->basePath = base_url().$this->config->item('CKEditor_path');
			$this->miniCKEditor->config['height'] = 126;
			$this->miniCKEditor->config['toolbar'] = 'basic';
			$this->miniCKEditor->config['language'] = 'zh-cn';
			CKFinder::SetupCKEditor($this->miniCKEditor,$this->config->item('CKFinder_path'));
			
			$this->load->view("admin/$this->adminCurrentTemplate/configEdit",$data);
		}		
	}
}

/* End of file config.php */
/* Location: ./application/controllers/admin/config.php */